<template>
    <div class="search">
      <input v-model="keyword" class="search-input" type="text" placeholder="输入城市名或拼音">
      <div class="search-content" ref="wrapper" v-show="keyword">
        <ul class="content">
          <li
            class="search-item b-1px-b"
            v-for="item of list"
            :key="item.id"
            @click.prevent="handleCityClick(item.name)"
          >{{item.name}}</li>
          <li class="search-item b-1px-b" v-show="hasNoData">没有找到匹配数据</li>
        </ul>
      </div>
    </div>
</template>

<script>
import Bscroll from 'better-scroll'
import {mapMutations} from 'vuex'
export default {
  name: 'CitySearch',
  props: {
    cities: Object
  },
  data  () {
    return {
      keyword: '',
      list: [],
      timer: null
    }
  },
  methods: {
    handleCityClick (city) {
      // this.$store.commit('changeCity2', city)
      this.changeCity2(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity2'])
  },
  computed: {
    hasNoData () {
      return !this.list.length
    }
  },
  watch: {
    // watch的重点不是return 返回值, computed的重点才是return返回值
    // keyword = return 值? 这里只是为了获取list而已, list[{}]装入的恰恰是符合要求的cities中的内容
    keyword () {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      if (!this.keyword) {
        this.list = []
        return
      }
      this.timer = setTimeout(() => {
        const result = []
        // 1.for...in循环遍历cities{}对象,获取的是key, cities[i]获取的是[]数组
        for (let i in this.cities) {
          // 2.forEach循环遍历数组A,类似于 A = [{},{},{}...], 获取的是{}对象, {id,spell,name}
          /* 整理一下逻辑
          for...in遍历对象, 获取的是key 和value, 其中value是数组, value = cities[key]
          forEach 遍历数组, 获取的是元素 + 索引, 其中item是对象{}
          arr.forEach((item,index,arr) => {}, thisValue)
          */
          this.cities[i].forEach((value) => {
            if (value.spell.indexOf(this.keyword) > -1 ||
                value.name.indexOf(this.keyword) > -1) {
              result.push(value)
            }
          })
        }
        this.list = result
      }, 100)
    },
    list () {
      this.$nextTick(() => {
        this.scroll = new Bscroll(this.$refs.wrapper, {
          click: true,
          mouseWheel: true
        })
      })
    }
  }
  // props异步 交给watch, input也交给watch, computed侧重复杂的计算
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .search
    height: .72rem
    background: $bgColor
    padding: 0  .1rem
    .search-input
      width 100%
      height: .62rem
      line-height .62rem
      text-align center
      border-radius .06rem
      color: #666
      padding 0 .1rem
      margin-top .02rem
      box-sizing border-box /*这个很重要, 容易影响 设置padding后是否能对齐*/
      touch-action none
    .search-content
      z-index 1 /*让其显示在其它内容之上*/
      overflow hidden   /*scroll重点, overflow + position*/
      position absolute
      top 1.58rem
      left 0
      right 0
      bottom 0
      background #eee
      .search-item
        line-height .62rem
        padding-left .2rem
        color #666
        background #fff
      .search-item
        &:after
          background #fefefe

</style>
